home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue59 / Clinic / UpdateDCI.dpr < prev   
Encoding:
Text File  |  2000-05-26  |  3.2 KB  |  106 lines

  1. program UpdateDCI;
  2.  
  3. uses
  4.   SysUtils,
  5.   Registry,
  6.   Windows,
  7.   Classes,
  8.   ShellAPI,
  9.   Dialogs,
  10.   Controls;
  11.  
  12. const
  13.   DelphiRegPath = 'Software\Borland\Delphi';
  14.   Delphi3 = '3.0';
  15.   Delphi4 = '4.0';
  16.   Delphi5 = '5.0';
  17.   CodeTemplateFileName = '\Bin\Delphi32.dci';
  18.   DelphiExe = '\Bin\Delphi32.exe';
  19.  
  20.   //Update these constants as appropriate
  21.  
  22.   //Your Code Template as stored in the DCI file (Name | Description)
  23.   Template = 'day | Comment for today';
  24.   //Your version of Delphi
  25.   MyDelphi = Delphi5;
  26.   //Your desired comment string, with a placeholder for the date
  27.   Comment = '%s - BL';
  28.   //Your preferred date format
  29.   DateFormat = 'd/mm/yyy';
  30.   //Where Delphi should start, relative to its root.
  31.   //Make sure you prefix with a \
  32.   DelphiStartDir = '\Projects';
  33.   //Custom command-line parameters for Delphi
  34.   DelphiStartParams = '/np /hm /hv';
  35.  
  36. var
  37.   DelphiRoot: String;
  38.   CodeTemplates: String;
  39.   TemplateStart, TemplateEnd: Integer;
  40.   Reg: TRegIniFile;
  41.   TemplateFile: TStream;
  42.  
  43. procedure RunDelphi;
  44. begin
  45.   ShellExecute(0, nil, PChar(DelphiRoot + DelphiExe),
  46.     DelphiStartParams, PChar(DelphiRoot + DelphiStartDir), SW_SHOWNORMAL)
  47. end;
  48.  
  49. begin
  50.   try //Application exception handler
  51.     Reg := TRegIniFile.Create('');
  52.     try
  53.       Reg.RootKey := HKEY_LOCAL_MACHINE;
  54.       if not Reg.OpenKey(DelphiRegPath, False) then
  55.         raise Exception.Create('Delphi information not found.');
  56.       DelphiRoot := Reg.ReadString(MyDelphi, 'RootDir', '');
  57.       if DelphiRoot = '' then
  58.         raise Exception.Create('Delphi root path not found.')
  59.     finally
  60.       Reg.Free
  61.     end;
  62.     //Read DCI file into a string
  63.     TemplateFile := TFileStream.Create(
  64.            DelphiRoot + CodeTemplateFileName,
  65.            fmOpenRead or fmShareDenyWrite);
  66.     try
  67.       SetLength(CodeTemplates, TemplateFile.Size);
  68.       TemplateFile.Read(CodeTemplates[1], TemplateFile.Size)
  69.     finally
  70.       TemplateFile.Free
  71.     end;
  72.     //Locate our template
  73.     TemplateStart := Pos(Template, CodeTemplates);
  74.     if TemplateStart = 0 then
  75.       raise Exception.Create('Custom Code Template not found.');
  76.     //Now locate the comment
  77.     while CodeTemplates[TemplateStart] <> '{' do
  78.       Inc(TemplateStart);
  79.     TemplateEnd := TemplateStart;
  80.     while CodeTemplates[TemplateEnd] <> '}' do
  81.       Inc(TemplateEnd);
  82.     //Substitute old date for today's date
  83.     Delete(CodeTemplates, TemplateStart, TemplateEnd - TemplateStart + 1);
  84.     Insert(Format('{ ' + Comment + ' }', [FormatDateTime(DateFormat, Date)]),
  85.       CodeTemplates, TemplateStart);
  86.     //Write DCI string back to file
  87.     TemplateFile := TFileStream.Create(
  88.            DelphiRoot + '\' + CodeTemplateFileName,
  89.            fmCreate);
  90.     try
  91.       TemplateFile.Write(CodeTemplates[1], Length(CodeTemplates));
  92.     finally
  93.       TemplateFile.Free
  94.     end
  95.   except
  96.     //If there is a problem, check it's ok to launch Delphi, otherwise leave
  97.     on E: Exception do
  98.       if MessageDlg(
  99.            Format('%s'#13#13'Continue loading Delphi', [E.Message]),
  100.            mtError, [mbOk, mbCancel], 0) = mrCancel then
  101.         Exit
  102.   end;
  103.   //Launch Delphi now template has been updated
  104.   RunDelphi
  105. end.
  106.